// 路由器
const express = require('express');
// 引入模型对象
const Users = require('../models/users');

const user_name_reg = /^[A-Za-z][A-Za-z0-9_]{2,19}$/;
const password_reg = /^[A-Za-z0-9_]{4,15}$/;
const router = new express.Router();


// 注册
router.post('/register', async (req, res) => {
    // console.log(req.body);
    const { user_name, password } = req.body;


    // console.log(user_name_reg.test(user_name));
    if (user_name_reg.test(user_name) && password_reg.test(password)) {
        // 
        try {
            await Users.create(req.body);
            res.send('注册成功***');
        } catch (e) {
            console.log(e);
            res.send('网络延迟，请重新注册');
        }


    } else {
        res.send('用户名或者密码不正确，请重新注册 ****');
    }

})

// 登录
router.post("/login", async (req, res) => {
    const { user_name, password } = req.body;
    if (user_name_reg.test(user_name) && password_reg.test(password)) {
        try {
            const user = await Users.findOne({ user_name });
            if (user) {
                if (user.password == password) {
                    res.send('登录成功');
                } else {
                    res.send('密码错误')
                }
            } else {
                res.send('用户不存在，请重新注册')
            }
        } catch (e) {
            console.log(e);
            res.send('网络延迟，错误');
        }
    } else {
        res.send('用户不存在');
    }
})

// 修改密码  方案一：通过post
router.post('/editPost', async (req, res) => {
    console.log(req.body);
    try {
        const result = await Users.updateOne({ user_name: req.body.user_name }, { $set: { password: req.body.password } });
        console.log(result);

        res.send('修改密码成功');
    } catch (e) {
        console.log(e);
        res.send('您的网络延迟，请稍后');
    }
})

// 方法二：get方式
// ?key=value&key=value
router.get('/editQuery', async (req, res) => {
    console.log(req.query);
    try {
        const result = await Users.updateOne({ user_name: req.query.user_name }, { $set: { password: req.query.password } });
        console.log(result);

        res.send('修改密码成功');
    } catch (e) {
        console.log(e);
        res.send('您的网络延迟，请稍后');
    }
})
// get请求的params参数
router.get('/editParams/:user_name/:password', async (req, res) => {
    console.log(req.params);
    try {
        const result = await Users.updateOne({ user_name: req.params.user_name }, { $set: { password: req.params.password } });
        console.log(result);

        res.send('修改密码成功');
    } catch (e) {
        console.log(e);
        res.send('您的网络延迟，请稍后');
    }
    res.send('test成功了');
})

// 删除密码
router.get('/del', async (req, res) => {
    console.log(req.query);
    try {
        const result = await Users.deleteOne({ user_name: req.query.user_name });
        console.log(result);
        res.send('删除成功');
    }catch(e){
        console.log(e);
        res.send('网络有延迟');
    }
    
})

// 测试
// router.get('/test',(req,res)=>{
//     console.log('test');
//     res.send('测试是否有问题');
// })

module.exports = router;